package com.guxingke.gsb.ui.cli;

import com.guxingke.gsb.Gsb;
import com.guxingke.gsb.util.PathUtil;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;
import org.kohsuke.args4j.ParserProperties;
import org.kohsuke.args4j.spi.BooleanOptionHandler;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 在当前目录执行初始化站点接口
 *
 * @author guxingke admin@guxingke.com
 */
public class InitCommand implements GsbCommand {
    @Argument(usage = "creates a new site at the given path", metaVar = "<path>")
    public List<String> args = new ArrayList<>();//

    Path filePath;

    @Option(name = "--help", aliases = {
            "-h"}, handler = BooleanOptionHandler.class, usage = "find help about this command")
    private boolean help = false;

    @Option(name = "-name", aliases = {
            "-n"}, metaVar = "<folder_name>", usage = "name of the directory to be created")
    private String name = "gsb";

    /**
     * Executes the command
     */
    @Override
    public void execute() {
        if (help) {
            System.out.println("Scaffold out a new Griffin directory structure.");
            System.out.println("usage: gsb new [option] <path>");
            System.out.println("Options: " + PathUtil.LINE_SEPARATOR);
            CmdLineParser parser = new CmdLineParser(this, ParserProperties.defaults().withUsageWidth(120));
            parser.printUsage(System.out);
            return;
        } else if (args.size() != 0) {
            filePath = Paths.get(args.get(0));
        } else {
            filePath = Paths.get(PathUtil.pathCheck(PathUtil.getClassRootPath()));
        }
        Gsb gsb = Gsb.getInstance();
        gsb.gsbInit(Paths.get(filePath.toString(),name));
        System.out.println("Successfully created new site.");
    }
}
